package net.huashitong.interview.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.interview.entity.NeedSolveProblem;
import net.huashitong.interview.entity.NeedSolveReadState;
import net.huashitong.interview.filter.NeedSolveProblemFilter;
import net.huashitong.interview.service.NeedSolveProblemService;
import net.huashitong.interview.service.ReadStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/NeedSolveProblem")
public class NeedSolveProblemController extends JsonBaseController {
    @Autowired
    private NeedSolveProblemService service;
    @Autowired
    private ReadStateService readStateService;
    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 需市里协调问题  首页  (加userId)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getindex",method = RequestMethod.POST)
    public Message getIndex(@RequestBody NeedSolveProblemFilter filter){
        String userId = filter.getUserId();
        String fillDate = "";
        if(ObjectUtils.isBlank(filter.getFillDate())){
            //获取数据库中最新的时间
            fillDate = service.getNewFillDate();
            if("0000".equals(fillDate)){
                return coverMessage("200", "暂无数据");
            }
            filter.setFillDate(fillDate.substring(0,6));
        }else{
            fillDate = filter.getFillDate();
        }
        List<NeedSolveProblem> list = service.queryNeedSolve(filter);
        if(list.size()==0){
            return coverMessage("200", "暂无数据");
        }
        List<Map<String, Object>> result = service.baseResult(list,userId);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fillDate",fillDate);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list",result);
        resultMap.put("time",map2);
        return coverMessage("200", "获取成功", resultMap);
    }

    /**
     * 需市里协调问题  详情页
     * @return
     */
    @Transactional
    @ResponseBody
    @RequestMapping(value = "/getdetailbyid",method = RequestMethod.POST)
    public Message getDetailById(@RequestBody NeedSolveProblemFilter filter){
        NeedSolveProblem needSolveProblem = service.getById(filter.getSysId());
        if(ObjectUtils.isBlank(needSolveProblem)){
            return coverMessage("200", "无详细信息");
        }
        NeedSolveReadState readState = readStateService.getBySolveIdAndUserId(filter.getSysId(),filter.getUserId());
        //更新状态
        if(!readStateService.getReadState(filter.getSysId(),filter.getUserId())){
            if(ObjectUtils.isBlank(readState)){
                NeedSolveReadState rs = new NeedSolveReadState();
                rs.setUserId(filter.getUserId());
                rs.setNeedSolveId(filter.getSysId());
                rs.setReadState("true");
                readStateService.save(rs);
            }else{
                readState.setUserId(filter.getUserId());
                readState.setNeedSolveId(filter.getSysId());
                readState.setReadState("true");
                readStateService.update(readState);
            }
        }

        Map<String,String> map = new HashMap<>();
        map.put("sysId",filter.getSysId());
        map.put("problemId",ObjectUtils.isNotBlank(needSolveProblem.getProblemId())?needSolveProblem.getProblemId():"-");
        map.put("problemBusiness",ObjectUtils.isNotBlank(needSolveProblem.getProblemBusiness())?needSolveProblem.getProblemBusiness():"-");
        map.put("fillDate",ObjectUtils.isNotBlank(needSolveProblem.getFillDate())?needSolveProblem.getFillDate():"-");
        map.put("fillUnitId",ObjectUtils.isNotBlank(needSolveProblem.getFillUnitId())?needSolveProblem.getFillUnitId():"-");
        map.put("specificProblem",ObjectUtils.isNotBlank(needSolveProblem.getSpecificProblem())?needSolveProblem.getSpecificProblem():"-");
        map.put("remark",ObjectUtils.isNotBlank(needSolveProblem.getRemark())?needSolveProblem.getRemark():"-");

        return coverMessage("200", "获取成功", needSolveProblem);
    }


    /**
     * 需市里协调问题  添加功能
     * @param needSolveProblem
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addneedsolveproblem")
    public Message addNeedSolveProblem(@RequestBody NeedSolveProblem needSolveProblem){
        String problemId = service.getNewProblemId();
        int xuhao = Integer.parseInt(problemId)+1;
        needSolveProblem.setProblemId(xuhao+"");
        service.save(needSolveProblem);
        return coverMessage("200", "添加成功");
    }

    /**
     * 需市里协调问题  修改功能
     * @param needSolveProblem
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editneedsolveproblem",method = RequestMethod.POST)
    public Message editNeedSolveProblem(@RequestBody NeedSolveProblem needSolveProblem){
        NeedSolveProblem newBean = service.getById(needSolveProblem.getSysId());
        if(ObjectUtils.isNotBlank(needSolveProblem.getFillDate())){
            newBean.setFillDate(needSolveProblem.getFillDate());
        }
        if(ObjectUtils.isNotBlank(needSolveProblem.getProblemBusiness())){
            newBean.setProblemBusiness(needSolveProblem.getProblemBusiness());
        }
        if(ObjectUtils.isNotBlank(needSolveProblem.getFillUnitId())){
            newBean.setFillUnitId(needSolveProblem.getFillUnitId());
        }
        if(ObjectUtils.isNotBlank(needSolveProblem.getSpecificProblem())){
            newBean.setSpecificProblem(needSolveProblem.getSpecificProblem());
        }
        if(ObjectUtils.isNotBlank(needSolveProblem.getProblemId())){
            newBean.setProblemId(needSolveProblem.getProblemId());
        }
        if(ObjectUtils.isNotBlank(needSolveProblem.getRemark())){
            newBean.setRemark(needSolveProblem.getRemark());
        }
        service.update(newBean);
        return coverMessage("200", "修改成功");
    }


    @ResponseBody
    @RequestMapping(value = "/deleteneedsolveproblem",method = RequestMethod.POST)
    public Message deleteNeedSolveProblem(@RequestBody NeedSolveProblem needSolveProblem){
        service.deleteById(needSolveProblem.getSysId());
        return coverMessage("200", "删除成功");
    }

}
